package io.xccit.hotelsystem.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import java.util.Date;

import io.xccit.hotelsystem.ui.login.LoginActivity;
import io.xccit.hotelsystem.ui.login.RegisterActivity;
import io.xccit.hotelsystem.ui.order.OrderFragment;

/**
 * 用户身份认证工具类
 */
public class AuthUtils {
    public static final String PREF_NAME = "UserAuth";
    public static final String KEY_USERNAME = "username";
    private static final String KEY_LOGIN_TIME = "login_time";
    private static final long EXPIRATION_TIME = 7 * 24 * 60 * 60 * 1000; // 7天

    /**
     * 检查用户是否已登录
     * @param context 上下文
     * @return 是否已登录
     */
    public static boolean isLoggedIn(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        String username = prefs.getString(KEY_USERNAME, null);
        long loginTime = prefs.getLong(KEY_LOGIN_TIME, 0);
        
        if (username == null || loginTime == 0) {
            return false;
        }
        
        // 检查是否过期
        long currentTime = new Date().getTime();
        if (currentTime - loginTime > EXPIRATION_TIME) {
            // 过期则清除登录信息
            clearLoginInfo(context);
            return false;
        }
        return true;
    }

    /**
     * 保存登录信息
     * @param context 上下文
     * @param username 用户名
     */
    public static void saveLoginInfo(Context context, String username) {
        SharedPreferences.Editor editor = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(KEY_USERNAME, username);
        editor.putLong(KEY_LOGIN_TIME, new Date().getTime());
        editor.apply();
        
        // 确保数据已提交
        editor.commit();
    }

    /**
     * 清除登录信息
     * @param context 上下文
     */
    public static void clearLoginInfo(Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE).edit();
        editor.remove(KEY_USERNAME);
        editor.remove(KEY_LOGIN_TIME);
        editor.apply();
    }

    /**
     * 检查登录状态并处理跳转
     * @param context 上下文
     * @param targetActivity 目标Activity的class
     * @return 是否已登录
     */
    public static boolean checkLoginAndRedirect(Context context, Class<?> targetActivity) {
        if (!isLoggedIn(context)) {
            new AlertDialog.Builder(context)
                    .setTitle("提示")
                    .setMessage("您未登录，请先登录，将前往登录页")
                    .setPositiveButton("确定", (dialog, which) -> {
                        Intent intent = new Intent(context, LoginActivity.class);
                        context.startActivity(intent);
                    })
                    .setCancelable(false)
                    .show();
            
            // 2秒后自动跳转
/*            new Handler().postDelayed(() -> {
                Intent intent = new Intent(context, LoginActivity.class);
                context.startActivity(intent);
            }, 2000);*/
            
            return false;
        }
        return true;
    }
    
    /**
     * 导航到登录页面
     * @param activity FragmentActivity实例
     */
    public static void navigateToLogin(FragmentActivity activity) {
        Intent intent = new Intent(activity, LoginActivity.class);
        activity.startActivity(intent);
    }
    
    /**
     * 导航到注册页面
     * @param activity FragmentActivity实例
     */
    public static void navigateToRegister(FragmentActivity activity) {
        Intent intent = new Intent(activity, RegisterActivity.class);
        activity.startActivity(intent);
    }

    /**
     * 导航到订单页面
     * @param fragmentActivity
     */
    public static void navigateToOrder(FragmentActivity fragmentActivity) {
        Intent intent = new Intent(fragmentActivity, OrderFragment.class);
        fragmentActivity.startActivity(intent);
    }
}